package th.stripes;

import able.stripes.JSP;
import com.google.inject.Inject;
import java.util.List;
import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLSession;
import javax.servlet.http.HttpSession;
import net.sourceforge.stripes.action.DefaultHandler;
import net.sourceforge.stripes.action.Resolution;
import net.sourceforge.stripes.action.UrlBinding;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import th.manager.PengiklananManager;
import th.manager.SequenceManager;
import th.manager.UserManager;
import th.model.Pengguna;
import th.model.Pengiklanan;
import th.model.SeqNum;

@UrlBinding("/welcome")
public class WelcomeActionBean extends BaseActionBean {

    private Pengguna pengguna;
    private static String AWAM = "001";
    
    @Inject SequenceManager sequenceManager;
    
    @Inject PengiklananManager iklanManager;
    
    @Inject UserManager userManager;
    
    private static String MSG_ID = "counter";
    
    private String counter;
    
    private List<Pengiklanan> senaraiIklan;
    
    public static Logger LOGGER = LoggerFactory.getLogger(WelcomeActionBean.class);
    
    static {        
        HttpsURLConnection.setDefaultHostnameVerifier(new HostnameVerifier() {
            @Override
            public boolean verify(String hostname, SSLSession session) {
                // ip address of the service URL(like.23.28.244.244)
                if (hostname.equals("210.187.87.180")) //temp
                {
                    return true;
                }
                return false;
            }
        });
    }
    

    @DefaultHandler
    public Resolution welcome() {
        pengguna = getContext().getCurrentUser();
        
        if (pengguna != null && pengguna.getJenisPengguna().getKod().equals(AWAM)) {
            String noPin = pengguna.getNoPin();
            String secPhrase = pengguna.getSecPhrase();
            if (StringUtils.isBlank(noPin)) {
                return new JSP("pguna/update_pin.jsp");
            } else if(StringUtils.isBlank(secPhrase)) {
                 return new JSP("pguna/update_security_info.jsp");
            }
        } 
        
        String msg = getContext().getRequest().getParameter("message");
        if (StringUtils.isNotBlank(msg)) {
            addSimpleError(msg);
        }
        
        counter = getContext().getCounter();
        if ("".equals(counter) || counter == null) {
            doCount();
        }
        
        senaraiIklan = iklanManager.senaraiIklan();
        return new JSP("home.jsp");
    }   
    
    private void doCount() {
        counter = sequenceManager.getSequenceWithoutLeadingZero(MSG_ID);
        SeqNum seqNum = new SeqNum();
        seqNum.setNum(Integer.parseInt(counter));
        seqNum.setUrusan(MSG_ID);  
        sequenceManager.updateCounter(seqNum);
        
        HttpSession ses = getContext().getRequest().getSession();

        ses.setAttribute(th.stripes.config.ableActionBeanContext.COUNTER_ID_KEY, counter);
    }
    
    public Resolution addCounter() {
        return new JSP("home.jsp");
    }    
    
    public Pengguna getPengguna() {
        return pengguna;
    }

    public void setPengguna(Pengguna pengguna) {
        this.pengguna = pengguna;
    }    

    public List<Pengiklanan> getSenaraiIklan() {
        return senaraiIklan;
    }

    public void setSenaraiIklan(List<Pengiklanan> senaraiIklan) {
        this.senaraiIklan = senaraiIklan;
    }
}
